package com.bsh.controller.goods;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.bsh.dto.CloudGoodsEditDto;
import com.bsh.dto.CloudGoodsListQryDto;
import com.bsh.mybatis.dataView.CloudGoodsInfo;
import com.bsh.mybatis.model.CloudGoods;
import com.bsh.service.CloudGoodsService;
import com.bsh.utils.PageJosnMessage;
import com.bsh.utils.PageTools;
import com.bsh.utils.Pager;
import com.bsh.utils.UploadUtils;
import com.mysql.jdbc.StringUtils;

@Controller
public class CloudGoodsController {
	private static Logger log = LoggerFactory.getLogger(CloudGoodsController.class);

	@Autowired
	HttpServletRequest request;
	@Autowired
	HttpServletResponse response;
	@Autowired
	CloudGoodsService goodsServ;



	/**
	 * 商品列表
	 */
	@RequestMapping(value = "/goods/cloudGoodsList")
	public ModelAndView goodsList(@ModelAttribute CloudGoodsListQryDto dto) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/goods/cloudGoodsList");

		String pageStr = request.getParameter("pageIndex");
		Integer pageIndex = Integer.parseInt(StringUtils.isNullOrEmpty(pageStr) ? "1" : pageStr);

		String sizeStr = request.getParameter("pageSize");
		Integer pageSize = Integer.parseInt(StringUtils.isNullOrEmpty(sizeStr) ? "10" : sizeStr);

		Pager pager = new Pager(pageIndex, pageSize);
		List<Map<String, Object>> list = goodsServ.getPage(pager, dto);
		mv.addObject("list", list);
		mv.addObject("pager", pager);
		mv.addObject("dto", dto);
		return mv;
	}

	/**
	 * 添加商品信息--初使化
	 */
	@RequestMapping(value = "/goods/add")
	public ModelAndView add(@ModelAttribute CloudGoodsEditDto dto) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/goods/cloudGoodsAdd");
		mv.addObject("goods", dto);
		return mv;
	}

	/**
	 * 添加商品信息--保存
	 */
	@RequestMapping(value = "/goods/add/save")
	public ModelAndView addSave(@ModelAttribute CloudGoodsEditDto dto,
			@RequestParam(value = "upimg") MultipartFile file,
			@RequestParam(value = "upimg2") MultipartFile fileSmall) {
		BeanCopier copy = BeanCopier.create(CloudGoodsEditDto.class, CloudGoods.class, false);
		CloudGoods goods = new CloudGoods();
		copy.copy(dto, goods, null);
		
		String fileUrl= "";
		if (!file.isEmpty()) {
			fileUrl = UploadUtils.save(file, "/upload/public/goods");
			goods.setImgBig(fileUrl);
		}	
		String fileUrlSmall= "";
		if (!fileSmall.isEmpty()) {
			fileUrlSmall = UploadUtils.save(fileSmall, "/upload/public/goods");
			goods.setImgSmall(fileUrlSmall);
		}		
		
		Integer res = goodsServ.addGoods(goods,dto.getMerchantName());

		ModelAndView mv = this.add(dto);

		String msg = "";
		if (res == -1)
			msg = PageTools.alert("添加商品信息", "保存失败，输入的供货商家不存在！", 1000);
		else if (res > 0)
			msg = PageTools.alertCloseFraRefresh("添加商品信息", "保存成功！", 1000);
		else
			msg = PageTools.alert("添加商品信息", "保存失败，请稍后重试！", 1000);

		mv.addObject("actionMsg", msg);
		return mv;
	}

	/**
	 * 修改商品信息--初使化
	 */
	@RequestMapping(value = "/goods/edit/{id}")
	public ModelAndView edit(@PathVariable(value = "id") Integer gid) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/goods/cloudGoodsEdit");
		CloudGoodsInfo goods = goodsServ.getGoods(gid);
		mv.addObject("goods", goods);
		return mv;
	}

	/**
	 * 修改商品信息--保存
	 */
	@RequestMapping(value = "/goods/edit/save")
	public ModelAndView editSave(@ModelAttribute CloudGoodsEditDto dto,
			@RequestParam(value = "upimg") MultipartFile file,
			@RequestParam(value = "upimg2") MultipartFile fileSmall) {
		BeanCopier copy = BeanCopier.create(CloudGoodsEditDto.class, CloudGoods.class, false);
		CloudGoods goods = new CloudGoods();
		copy.copy(dto, goods, null);
		
		String fileUrl= "";
		if (!file.isEmpty()) {
			fileUrl = UploadUtils.save(file, "/upload/public/goods");
			goods.setImgBig(fileUrl);
		}	
		String fileUrlSmall= "";
		if (!fileSmall.isEmpty()) {
			fileUrlSmall = UploadUtils.save(fileSmall, "/upload/public/goods");
			goods.setImgSmall(fileUrlSmall);
		}		
		Integer res = goodsServ.editGoods(goods,dto.getMerchantName().trim()); 

		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/goods/cloudGoodsEdit");
		mv.addObject("goods", dto);

		String msg = "";
		if (res==-1)
			msg = PageTools.alert("修改商品信息", "没有查询到商品，保存失败！", 1000);
		else if (res > 0)
			msg = PageTools.alertCloseFraRefresh("修改商品信息", "保存成功！", 1000);
		else
			msg = PageTools.alert("修改商品信息", "保存失败，请稍后重试！", 1000);

		mv.addObject("actionMsg", msg);
		return mv;
	}
	
	/**
	 * 下架
	 */
	@ResponseBody
	@RequestMapping(value = "/goods/enable/")
	public PageJosnMessage enable(@RequestParam(value="id") int id, @RequestParam(value="isEnable") int isEnable) {
		Integer res = goodsServ.enable(id, isEnable == 1);
		PageJosnMessage msg = new PageJosnMessage();
		res = (res == null ? 1 : res);
		msg.putData("status", res > 0 ? 1 : 0);
		return msg;
	}
}
